import {useCallback} from "react";
import {Modal, message} from "antd";
import {ApiCreateOrEditInfo, ApiDelInfo} from "@/api";
interface KeysProps {
    pkId?: any;
}
interface UseCardPrrops {
    createApi?: (e?: any) => {};
    delApi?: (e?: any) => {};
}

export default function useCard({createApi, delApi}: UseCardPrrops): any {
    const onDel = useCallback((keys: KeysProps[], callBack: () => void) => {
        Modal.warning({
            title: "是否删除",
            okText: "确定",
            onOk: async () => {
                try {
                    let res: any = "";
                    if (delApi) {
                        res = await delApi(keys);
                    } else {
                        res = await ApiDelInfo(keys);
                    }
                    console.log(res);

                    if (res.code == "OK") {
                        message.success("删除成功");
                        callBack && callBack();
                    }
                } catch (error) {
                    message.success("删除失败");
                }
            },
        });
    }, []);

    const onCreateOrEdit = async (keys: KeysProps[]) => {
        try {
            let res: any = "";
            if (createApi) {
                res = await createApi(keys);
            } else {
                res = await ApiCreateOrEditInfo(keys);
            }
            message.success("编辑成功");
            return res;
        } catch (error) {
            message.error("编辑失败");
        }
    };

    return [onDel, onCreateOrEdit];
}
